package org.kia.widgets;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Message;
import android.util.AttributeSet;
import android.widget.ImageView;
import com.github.droidfu.imageloader.ImageLoader;
import com.github.droidfu.imageloader.ImageLoaderHandler;

// TODO:
// 1. Support instantiation from xml (need to get the image url)
// 2. Try making things more asynchronously

/**
 * @author Amir Lazarovich
 * @version 1.0.6
 */
public class SimpleWebImageView extends ImageView {

    public static final int NO_IMAGE = -1;

    private String mImageUrl;
    private int mDefaultImageId;
    protected boolean isLoaded;

    protected ScaleType scaleType = ScaleType.CENTER_CROP;

    public SimpleWebImageView(Context context) {
        super(context);
        init(context, null, NO_IMAGE, false);
    }

    public SimpleWebImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, null, NO_IMAGE, false);
    }

    public SimpleWebImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, null, NO_IMAGE, false);
    }

    public SimpleWebImageView(Context context, String imageUrl, Integer defaultImageId, boolean autoLoad) {
        super(context);
        init(context, imageUrl, defaultImageId, autoLoad);
    }

    protected void init(Context context, String imageUrl, Integer defaultImageId, boolean autoLoad) {
        if (defaultImageId != NO_IMAGE) {
            setImageDrawable(context.getResources().getDrawable(defaultImageId));
        }

        mImageUrl = imageUrl;
        mDefaultImageId = defaultImageId;
        ImageLoader.initialize(context);

        if (autoLoad && imageUrl != null) {
            loadImage();
        }
    }

    public String getImageUrl() {
        return mImageUrl;
    }

    @Override
    public void setImageURI(Uri uri) {
        mImageUrl = uri.toString();
        super.setImageURI(uri);
    }

    public void setImageUrl(String url) {
        mImageUrl = url;
        loadImage();
    }

    /**
     * Use this method to trigger the image download if you had previously set autoLoad to false.
     */
    public void loadImage() {
        if (mImageUrl == null) {
            throw new IllegalStateException("image URL is null; did you forget to set it for this view?");
        }
        ImageLoader.start(mImageUrl, new DefaultImageLoaderHandler());
    }

    /**
     * Check if image has loaded
     *
     * @return
     */
    public boolean isLoaded() {
        return isLoaded;
    }

    /**
     * Often you have resources which usually have an image, but some don't. For these cases, use this method to supply a
     * placeholder drawable which will be loaded instead of a web image.
     *
     * @param imageResourceId the resource of the placeholder image drawable
     */
    public void setDefaultImageById(int imageResourceId) {
        setImageDrawable(getResources().getDrawable(imageResourceId));
        mDefaultImageId = imageResourceId;
    }

    /**
     * Reset image to default
     */
    public void reset() {
        setImageDrawable(getResources().getDrawable(mDefaultImageId));
    }

    protected class DefaultImageLoaderHandler extends ImageLoaderHandler {

        public DefaultImageLoaderHandler() {
            super(SimpleWebImageView.this, mImageUrl, null);
        }

        @Override
        protected boolean handleImageLoaded(Bitmap bitmap, Message msg) {
            boolean wasUpdated = super.handleImageLoaded(bitmap, msg);
            if (wasUpdated) {
                isLoaded = true;
            }
            return wasUpdated;
        }
    }

    /**
     * Get last assigned image url
     *
     * @return
     */
    public String getLastImageUrl() {
        return (String) getTag();
    }

}
